import cv2
import matplotlib.pyplot as plt
import numpy as np

#数值计算
#img_new = cv2.imread("new.jpg")
#img_lu = cv2.imread("lu.jpg")
#img_new2 = img_new*10
#img_lu + img_new
#img_lu.shape
#img_new = cv2.resize(img_new, (556, 850))
#img_new.shape
#res = cv2.addWeighted(img_lu, 0.4, img_new,  0.6, 0)
#plt.imshow(res)

#res = cv2.resize(img_lu, (0, 0), fx=3, fy=1)
#plt.imshow(res)

#腐蚀

#img = cv2.imread("mao.jpg")
#cv2.imshow("img", img)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

#kernel = np.ones((5,5),np.uint8)
#erosion = cv2.erode(img,kernel,iterations = 1)

#cv2.imshow('erosion', erosion)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
#mao = cv2.imread('mao.jpg')

#cv2.imshow('mao', mao)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

#kernel = np.ones((5,5),np.uint8)
#erosion_1 = cv2.erode(mao,kernel,iterations = 1)
#erosion_2 = cv2.erode(mao,kernel,iterations = 2)
#erosion_3 = cv2.erode(mao,kernel,iterations = 3)
#res = np.hstack((erosion_1,erosion_2,erosion_3))
#cv2.imshow('res', res)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

#膨胀

#img = cv2.imread('mao.jpg')
#cv2.imshow('img', img)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
#kernel = np.ones((5,5),np.uint8)
#dige_erosion = cv2.erode(img,kernel,iterations = 1)

#erosion = cv2.erode(img,kernel,iterations = 2)
#cv2.imshow('erosion', erosion)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
#kernel = np.ones((3,3),np.uint8)
#dige_dilate = cv2.dilate(dige_erosion,kernel,iterations = 2)

#cv2.imshow('dilate', dige_dilate)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
#mao = cv2.imread('mao.jpg')

#kernel = np.ones((10,10),np.uint8)
#dilate_1 = cv2.dilate(mao,kernel,iterations = 1)
#dilate_2 = cv2.dilate(mao,kernel,iterations = 2)
#dilate_3 = cv2.dilate(mao,kernel,iterations = 3)
#res = np.hstack((dilate_1,dilate_2,dilate_3))
#cv2.imshow('res', res)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

#开运算和闭能算

# 开：先腐蚀，再膨胀
#img = cv2.imread('mao.jpg')

#kernel = np.ones((6,6),np.uint8)
#opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

#cv2.imshow('opening', opening)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

# 闭：先膨胀，再腐蚀
#img = cv2.imread('mao.jpg')

#kernel = np.ones((6,6),np.uint8)
#closing = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)

#cv2.imshow('closing', closing)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

#梯度运算

# 梯度=膨胀-腐蚀
#mao = cv2.imread('mao.jpg')
#kernel = np.ones((7,7),np.uint8)
#dilate = cv2.dilate(mao,kernel,iterations = 5)
#erosion = cv2.erode(mao,kernel,iterations = 5)

#res = np.hstack((dilate,erosion))

#cv2.imshow('res', res)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

#gradient = cv2.morphologyEx(mao, cv2.MORPH_GRADIENT, kernel)

#cv2.imshow('gradient', gradient)
#cv2.waitKey(0)
#cv2.destroyAllWindows()

#礼帽和黑帽

#礼帽
img = cv2.imread('mao.jpg')
kernel = np.ones((6,6),np.uint8)
tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel)
cv2.imshow('tophat', tophat)
cv2.waitKey(0)
cv2.destroyAllWindows()

#黑帽
img = cv2.imread('mao.jpg')
kernel = np.ones((6,6),np.uint8)
blackhat  = cv2.morphologyEx(img,cv2.MORPH_BLACKHAT, kernel)
cv2.imshow('blackhat ', blackhat )
cv2.waitKey(0)
cv2.destroyAllWindows()